package com.song;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;

public class TestMath {
	
	public static void main(String[] args) {
		//readDir("");
		readDirMax("D:/config/pei/40nmAu500X-1");
	}
	
	public static String readDirMax(String dir)
	{

		List<PostionInfo> pos = new ArrayList<PostionInfo>();
		StringBuffer sb = new StringBuffer();
        List<File> files = new ArrayList<File>();
        
		File fdir = new File(dir);
		if(fdir.isDirectory())
		{
			for(File file :fdir.listFiles())
			{
				files.add(file);
			}
		}else{
			files.add(fdir);
		}
		for(File file :files)
		{
			String filepath = file.getAbsolutePath();
			PostionInfo info =  getMaxPostionInfoFile(filepath);
			sb.append(file.getName() +"------"+info.toString() +"\r\n");
			System.out.println(file.getName() +"------"+info.toString());
		}
		return sb.toString();
	
	}
	
	public static String readDir(String dir , String points)
	{
		List<PostionInfo> pos = new ArrayList<PostionInfo>();
		StringBuffer sb = new StringBuffer();
                String[] segs =  points.split(";");
                for(String seg :segs)
                {
                    PostionInfo p = new PostionInfo();
                    seg = seg.replaceAll("\\(|\\)", "");
                    String[] arr = seg.split(",");
                    p.setX(Integer.parseInt(arr[0]));
                    p.setY(Integer.parseInt(arr[1]));
                    pos.add(p);
                }
        List<File> files = new ArrayList<File>();
        
		File fdir = new File(dir);
		if(fdir.isDirectory())
		{
			for(File file :fdir.listFiles())
			{
				files.add(file);
			}
		}else{
			files.add(fdir);
		}
		for(File file :files)
		{
			String filepath = file.getAbsolutePath();
			getPostionInfoByPosFile(filepath ,pos);
			sb.append(file.getName() +"------"+pos.toString() +"\r\n");
			System.out.println(file.getName() +"------"+pos.toString());
		}
		return sb.toString();
	}
	
	public static String readDirAVG(String dir , String points)
	{
		List<PostionInfo> pos = new ArrayList<PostionInfo>();
		StringBuffer sb = new StringBuffer();
                String[] segs =  points.split(";");
                for(String seg :segs)
                {
                    PostionInfo p = new PostionInfo();
                    seg = seg.replaceAll("\\(|\\)", "");
                    String[] arr = seg.split(",");
                    p.setX(Integer.parseInt(arr[0]));
                    p.setY(Integer.parseInt(arr[1]));
                    pos.add(p);
                }
        List<File> files = new ArrayList<File>();
        
		File fdir = new File(dir);
		if(fdir.isDirectory())
		{
			for(File file :fdir.listFiles())
			{
				files.add(file);
			}
		}else{
			files.add(fdir);
		}
		for(File file :files)
		{
			String filepath = file.getAbsolutePath();
			Double avg = getPointAvgByPosFile(filepath ,pos);
			sb.append(file.getName() +"------"+avg.toString() +"\r\n");
			System.out.println(file.getName() +"------"+avg.toString());
		}
		return sb.toString();
	}
	
	
	
	
	public static PostionInfo getMaxPostionInfoFile(String filepath )
	{
		List<String[]> datas = readFilePath(filepath );
		return getMaxPostionInfo(datas);
	}
	
	public static void getPostionInfoByPosFile(String filepath , List<PostionInfo> pos)
	{
		List<String[]> datas = readFilePath(filepath );
		getPostionInfoListByPos(datas ,pos);
	}
	
	public static Double getPointAvgByPosFile(String filepath , List<PostionInfo> pos)
	{
		List<String[]> datas = readFilePath(filepath );
		return getPostionInfoAvgByPos(datas ,pos);
		
	}
	
	private static Double getPostionInfoAvgByPos(List<String[]> datas,
			List<PostionInfo> pos) {
		
		Integer[] xrang = new Integer[]{ pos.get(0).getX() , pos.get(1).getX()};
		Integer[] yrang = new Integer[]{ pos.get(0).getY() , pos.get(1).getY()};
		float total =0 , count = 0;
		for(int i = xrang[0] -1 ; i<xrang[1] ;i++)
		{
			for(int j = yrang[0] ; j<=yrang[1];j++)
			{
				int data = Integer.parseInt(datas.get(i)[j]);
				System.out.println(data+"");
				total += data;
				count++;
			}
		}
		
		if(count >0)
		{
			return (total * 1.0)/ count;
		}
		return 0.0;
	}


	public static  List<String[]> readFilePath(String path )
	{
		List<String[]> datas = new ArrayList<String[]>();
		try {
			List<String> lines = IOUtils.readLines(new FileInputStream(new File(path)));
			for(String line :lines)
			{
				String[] segs = line.split("\t");
				datas.add(segs);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return datas;
	}
	
	public static PostionInfo  getMaxPostionInfo(List<String[]> datas)
	{
		PostionInfo info = new PostionInfo();
		for(int i = 0 ; i<datas.size() ;i++)
		{
			for(int j= 1 ;j<datas.get(i).length  ;j++)
			{
				if(Integer.parseInt(datas.get(i)[j])>info.getData())
				{
					info.setX(i);
					info.setY(j);
					info.setData(Integer.parseInt(datas.get(i)[j]));
				}
			}
		}
		info.setX( info.getX() +1);
		return info;
	}
	
	public static void getPostionInfoListByPos(List<String[]> datas , List<PostionInfo> pos)
	{
		for(PostionInfo info :pos)
		{
			info.setData(Integer.parseInt(datas.get(info.getX()-1)[info.getY()]) );
		}
	}
        
        
        public static double segAvg(String datas)
        {
            if(null == datas || "".equals(datas))
            {
                return 0;
            }
            String[] segs = datas.split("(\t| |\n)");
            int total = 0 ,count = 0 ;
            for(String seg :segs)
            {
                if(null!=seg && !"".equals(seg))
                {
                    int num = Integer.parseInt(seg);
                    total += num;
                    count++;
                }
            }
            
            if(count > 0)
            {
                return (total * 1.0)/count;
            }
            return 0;
        }
        
	
}




